package com.sls.web.manager.action.recharge;

import java.io.IOException;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.Supplier;
import com.sls.web.manager.recharge.querybean.SupplierQuery;
import com.sls.web.manager.recharge.service.SupplierService;

public class SupplierAction extends SuperAction {
	private static final long serialVersionUID = 7039471396012254950L;
	@Resource(name = "supplierService")
	private SupplierService supplierService;
	/**
	 * 返回分页对象
	 */
	public Pagination<?> page;
	private SupplierQuery supplierQuery;
	private Supplier supplier;
	private String id;
	@JSON(serialize = false)
	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}
	@JSON(serialize = false)
	public SupplierQuery getSupplierQuery() {
		return supplierQuery;
	}

	public void setSupplierQuery(SupplierQuery supplierQuery) {
		this.supplierQuery = supplierQuery;
	}
	@JSON(serialize = false)
	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}
	@JSON(serialize = false)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	// 添加
	public String add() {
		return "edit";
	}

	// 添加供货商
	public String addSupplier() throws Exception {
		try {
			supplierService.addSupplier(supplier, this.getCurrentOperator());
			redirectionUrl = "supplier!list.htm";
			return "success";
		} catch (ManagerException e) {
			supplier = null;
			addActionError(e.getMessage());
			return this.add();
		} catch (Exception e) {
			addActionError("系统忙！");
			return this.add();
		}

	}

	// 列表
	public String list() {
		if (supplierQuery == null) {
			supplierQuery = new SupplierQuery();
			supplierQuery.setPageSize(Constants.INIT_PAGESIZE);
			supplierQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			page = supplierService.querySupplier(supplierQuery,
					getCurrentOperator());
		} catch (Exception e) {
			return ERROR;
		}

		return "list";
	}

	// 删除
	public String delete() throws Exception {
		try{
			supplierService.delete(id, getCurrentOperator());
			redirectionUrl = "supplier!list.htm";
			return "success";
		}catch(ManagerException e){
			addActionError(e.getMessage());
			return this.list();			
		}
	}

	// 编辑
	public String edit() throws Exception {
		if (id != null) {
			supplier = supplierService.querySupplierById(id,
					this.getCurrentOperator());
		}
		return "edit";
	}

	// 编辑供货商
	public String editSupplier() throws Exception {
		boolean result = supplierService.updateSupplier(supplier,
				this.getCurrentOperator());
		if (!result) {
			addActionError("修改失败!");
			return this.edit();
		} else {
			redirectionUrl = "supplier!list.htm";
			return "success";
		}
	}
	
	// 校验注册接口名称是否已存在
	public String checkName() throws IOException {
		message = String.valueOf(supplierService.checkName(supplier,
				this.getCurrentOperator()));
		return "ajax";
	}
}